﻿//
// MainPage.xaml.cpp
// Implementation of the MainPage class
//

#include "pch.h"
#include "MainPage.xaml.h"

using namespace Coding4Fun::Maelstrom::GameConsole;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::System;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Core;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

MainPage::MainPage()
{
	InitializeComponent();

	CoreWindow::GetForCurrentThread()->KeyDown += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &MainPage::OnKeyDown);
	CoreWindow::GetForCurrentThread()->PointerMoved += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &MainPage::OnPointerMoved);
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.  The Parameter
/// property is typically used to configure the page.</param>
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
	(void) e;	// Unused parameter
}

void Coding4Fun::Maelstrom::GameConsole::MainPage::InitializeInputs(_In_ GameEngine^ game)
{
	m_game = game;

	Window::Current->CoreWindow->PointerCursor = nullptr;
}

void Coding4Fun::Maelstrom::GameConsole::MainPage::OnKeyDown(_In_ CoreWindow^, _In_ KeyEventArgs^ e)
{
	switch(e->VirtualKey)
	{
		case VirtualKey::F1:
			if(AdminPanel->Visibility == Windows::UI::Xaml::Visibility::Collapsed)
				AdminPanel->Visibility = Windows::UI::Xaml::Visibility::Visible;
			else
				AdminPanel->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
			break;
		case VirtualKey::F2:
			m_game->SetGameState(Coding4Fun::Maelstrom::Common::GameState::Countdown);
			break;
		case VirtualKey::F3:
			m_game->SetGameState(Coding4Fun::Maelstrom::Common::GameState::WaitingRoom);
			break;
		case VirtualKey::F4:
			m_game->ConnectToConsole1();
			break;
		case VirtualKey::F5:
			m_game->FirePuck(2, Coding4Fun::Maelstrom::Common::Player::One);
			break;
		case VirtualKey::F9:
			m_game->SetGameState(Coding4Fun::Maelstrom::Common::GameState::GameResults);
			break;
		case VirtualKey::F8:
			m_game->SplitPucks();
			break;
		case VirtualKey::F12:
			m_game->MjpegStation(true);
			break;
	}
}

void Coding4Fun::Maelstrom::GameConsole::MainPage::OnPointerMoved(_In_ CoreWindow^, _In_ PointerEventArgs^ e)
{
	if (!m_game->IsLoaded())
		return;

	float normalX = e->CurrentPoint->Position.X / 1920.0f;
	float normalY = e->CurrentPoint->Position.Y / 1080.0f;
	m_game->Paddle1()->NormalizedPosition(XMFLOAT3(normalX, normalY, m_game->Paddle1()->NormalizedPosition().z));
	m_game->Paddle2()->NormalizedPosition(XMFLOAT3(1.0f-normalX, normalY, m_game->Paddle1()->NormalizedPosition().z));
}

void MainPage::SetOutputText(String^ msg)
{
	if(!Dispatcher->HasThreadAccess)
	{
		Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([=]()
		{
			txtOut->Text = msg;
		}));
	}
	else
		txtOut->Text = msg;
}

void MainPage::SetFPSText(String^ msg)
{
	//msg += m_game->IsPlayerOne() ? " (P1)" : " (P2)";

	if(!Dispatcher->HasThreadAccess)
	{
		Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([=]()
		{
			fpsOut->Text = msg;
		}));
	}
	else
		fpsOut->Text = msg;
}
